<?php

	class cct_member extends ctl_member
	{
		function pptDownload($order_id)
		{	
			$objOrder = $this->system->loadModel('trading/order');
			$order = $objOrder->getFieldById($order_id);
			// 防止恶意退款 然后记下链接下载
			if ($order['pay_status'] != 1)
			{
				$this->splash('failed',$this->system->mkUrl('member','orders'),__('订单未付款或者该订单不存在'));
				//exit('订单未付款或者该订单不存在');
			}
			/* 一个订单多个商品 */
			$aOrder = $objOrder->getItemsFieldById($order_id,array('product_id','name'),false);
			if (count($aOrder) > 1)
			{
				$tar = $this->system->loadModel('utility/tar');
				$workdir = getcwd();
				
				foreach ($aOrder as $key=>$var)
				{
					if (strstr($var['name'], '静态模板'))
					{
						$field = 'static_ppt';
						$ppt_type = 'static';
					}
					elseif (strstr($var['name'], '动态模板'))
					{
						$field = 'dynamic_ppt';
						$ppt_type = 'dynamic';
					}

					$objGoods = $this->system->loadModel('trading/goods');
					$aGoods = $objGoods->getFieldByProductId($var['product_id'],array('goods_id'));
					$aData = $objGoods->getFieldById($aGoods['goods_id'],array($field));
	
					if (chdir(HOME_DIR.'/upload/ppt'))
					{
						$tar->addFile($ppt_type.'/'.$aData[$field]);
					}
					else chdir($workdir);
				}
				
				$name = time();
				@set_time_limit(0);
				//header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
				header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
				header('Content-type: application/octet-stream');
				header('Content-type: application/force-download');
				header('Content-Disposition: attachment; filename="'.$name.'.tgz"');
				$tar->getTar('output');
				chdir($workdir);
			}
			else
			{
				//一个订单一个商品
				$aOrder = $objOrder->getItemsFieldById($order_id,array('product_id','name'));		
				
				if (strstr($aOrder['name'], '静态模板'))
				{
					$field = 'static_ppt';
					$pptUrlPrefix = HOME_DIR.'/upload/ppt/static/';
				}
				elseif (strstr($aOrder['name'], '动态模板'))
				{
					$field = 'dynamic_ppt';
					$pptUrlPrefix = HOME_DIR.'/upload/ppt/dynamic/';
				}
				$objGoods = $this->system->loadModel('trading/goods');
				$aGoods = $objGoods->getFieldByProductId($aOrder['product_id'],array('goods_id'));
				$aData = $objGoods->getFieldById($aGoods['goods_id'],array($field));

				$filePath = $pptUrlPrefix.$aData[$field];
				
				if (false !== ($pos=strrpos($aData[$field],'.')))
				{
					$ext = substr($aData[$field],$pos);
				}

				$charset = $this->system->loadModel('utility/charset');
				$name = $charset->utf2local($aData[$field],'zh');
				@set_time_limit(0);
				header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
				header('Content-type: application/octet-stream');
				header('Content-type: application/force-download');
				header('Content-Disposition: attachment; filename="'.$name.'"');
				$content = file_get_contents($filePath);
				echo $content;
			}
		}
	}


?>